package impl

import (
	"context"
	"fmt"

	"gitee.com/lcc_dr/restful-api-demo/apps/host"
	"github.com/infraboard/mcube/logger"
)

func (h *HostServiceImpl) CreateHost(ctx context.Context, ins *host.Host) (*host.Host, error) {

	h.l.Debug("create host")
	// 带Format的日志打印, fmt.Sprintf()
	h.l.Debugf("create host %s", ins.Name)
	h.l.Debugf("create host cpu is  %d", ins.CPU)
	// 携带额外meta数据, 常用于Trace系统
	h.l.With(logger.NewAny("request-id", "req01")).Debug("create host with meta kv")
	h.l.Info("create host ")
	fmt.Println("host is created")
	_, err := h.createHost(ctx, ins)
	if err != nil {

		//http error 返回处理
		return ins, err
	}

	return ins, nil
}

func (h *HostServiceImpl) QueryHosts(ctx context.Context, req *host.QueryHostsRequest) (*host.HostSet, error) {

	hosts, err := h.queryHosts(ctx, req)
	if err != nil {
		return nil, err
	}
	return hosts, nil
}

func (h *HostServiceImpl) DescribeHost(ctx context.Context, req *host.DescribeHostRequest) (*host.Host, error) {

	host, err := h.querydescribeHost(ctx, req)

	if err != nil {
		return nil, err
	}
	return host, nil
}

func (h *HostServiceImpl) UpdatePatchHost(ctx context.Context, req *host.UpdatePatchHostRequest) (*host.Host, error) {
	host, err := h.updatePatchHost(ctx, req)
	if err != nil {
		return nil, err
	}
	return host, nil
}

func (h *HostServiceImpl) UpdatePutHost(ctx context.Context, req *host.UpdatePutHostRequest) (*host.Host, error) {
	host, err := h.updatePutHost(ctx, req)
	if err != nil {
		return nil, err
	}
	return host, nil
}

func (h *HostServiceImpl) DeleteHost(ctx context.Context, req *host.DeleteHostRequest) (*host.Host, error) {

	host, err := h.deleteHost(ctx, req)
	if err != nil {
		return nil, err
	}
	return host, nil

}
